﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Windvoice.Blog;
using Castle.ActiveRecord.Framework;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Framework.Config;
using System.Xml;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Collections;
using System.IO;

namespace Windvoice.TestConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, string> dict = new Dictionary<string, string>() { 
                {"ie6","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.2; .NET CLR 2.0.50727)"},
                {"chrome","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11"},
                {"firefox","Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.27) Gecko/20120216 Firefox/3.6.27"},
                {"ie8","Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; QQDownload 677; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0E; .NET4.0C)"}
            };

            foreach (var key in dict.Keys)
            {
                var test = dict[key];
                var match = Regex.Match(test, @"MSIE (6[\.0-9]{0,})");

                Console.WriteLine("{0}:{1}", key, (test.ToLower().Contains("msie") && !test.ToLower().Contains("opera") && match.Groups.Count > 0 && match.Groups[0].Success));
            }

            Console.Read();

            return;


            XmlConfigurationSource source = new XmlConfigurationSource("config/castle.xml");
            var Assemblies = new Assembly[] { System.Reflection.Assembly.GetAssembly(typeof(Windvoice.Blog.Article)) };
            ActiveRecordStarter.Initialize(Assemblies, source);
            
            //Category cat = new Category()
            //{
            //    Name = "aa",
            //    Description = "bb"
            //};

            //cat.Create();
            //Console.WriteLine("created!");

            //k:
            //int PageIndex = 0;
            //Console.WriteLine("input:");
            //string input = Console.ReadLine();

            //int.TryParse(input, out PageIndex);
            //Console.WriteLine(PageIndex);

            //goto k;
            int temp = 0;
            var articles = Article.GetList(1,int.MaxValue,0,out temp);
            //var a = Article.Find(171);
            var sw = new StreamWriter(@"e:\temp\articles.txt") {AutoFlush=true };
            //foreach (var a in articles)
            //{
            var a = articles[12];
                var content = Windvoice.Library.HtmlHelper.HtmlSummary(a.Content, 300);

                Console.WriteLine(a.Title);
                sw.WriteLine(a.Title);
                Console.WriteLine(content);
                //sw.WriteLine(a.Content);
                //sw.WriteLine(String.Empty);
                sw.WriteLine(content);

                Console.WriteLine(String.Empty);
                sw.WriteLine(String.Empty);
            //}

            sw.Flush();
            sw.Close();
            Console.WriteLine("finished");
            Console.Read();
        }

        public static string HtmlSummary(string content, int length)
        {
            //如果输入字符串长度小于截取长度，直接返回
            if (content.Length <= length) return content;
            //如果输入字符串竟未包含html代码，那就直接截取
            if (!content.Contains("<")) return content.Substring(0, length);
            //如果输入字符串中包含手动截取标记，则返回标记前的内容
            if (content.Contains("<!-- summary end -->")) return Regex.Split(content, "<!-- summary end -->")[0];

            var IsTag = false; //当前字符是否在标签中
            var Summary = String.Empty; //截取的子字符串
            var TagName = String.Empty; //当前标签名称
            var TagArray = new { Left = new ArrayList(), Right = new ArrayList() }; //左标签数组、右标签数组

            foreach (var current in content)
            {
                if (length <= 0) break; //在截取长度>0的情况下，循环遍历html字符

                if (current == '<') //当前字符为<，进入html标签
                {
                    IsTag = true;
                }
                else if (current == '>') //当前字符为>，移出html标签
                {
                    //如果当前标签名称以/开头，则此标签为闭合标签，应加入右标签数组
                    if (TagName.Trim().StartsWith("/")) 
                        TagArray.Right.Add(TagName);
                    //否则应加入左标签数组，但应考虑自闭合标签不加入闭合标签数组
                    else if (!TagName.Trim().EndsWith("/") && !"img|br|input".Contains(TagName.ToLower()))
                        TagArray.Left.Add(TagName.Trim().ToLower().Split(' ')[0]);

                    IsTag = false; //标记已移出html标签
                    TagName = String.Empty; //完成标签加入标签数组后，应重置此变量
                }
                else 
                {
                    if (IsTag) //如果在html标签中，则拼积当前标签名称
                        TagName += current;
                    else //否则为纯文本内容，计数器开始自减，直到减为0，循环退出
                        length--;
                }

                Summary += current; //拼积截取的子字符串
            }

            //从左标签数组中移除已闭合的标签
            TagArray.Left.RemoveRange(0, TagArray.Right.Count);

            //剩余标签未闭合，逆序以html闭合标签格式拼接至截取的子字符串末尾
            if (TagArray.Left.Count > 0)
                for (var i = TagArray.Left.Count - 1; i >= 0; i--)
                {
                    Summary += String.Format("</{0}>", TagArray.Left[i].ToString());
                }

            return Summary;
        }
    }
}
